package br.com.sysprova.controller;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 
 * @author isaac
 */
public class Criptografia {
	
	private static Criptografia cript;
	private static Cipher cipher;
	private static final String CHAVE = "6dir610ml016j7kl";
	
	private Criptografia(){
		
	}
	
	public static Criptografia getInstance(){
		if (cript==null) {
			cript = new Criptografia();
		}
		return cript;
	}

	public byte[] critografa(String texto) throws InvalidKeyException,
			IllegalBlockSizeException, BadPaddingException,
			NoSuchAlgorithmException, NoSuchPaddingException {
		cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(CHAVE.getBytes(),
				"AES"));
		byte[] encrypted = cipher.doFinal(texto.getBytes());
		return encrypted;

	}

	public String descritografa(byte[] critografa)
			throws InvalidKeyException, IllegalBlockSizeException,
			BadPaddingException, NoSuchAlgorithmException,
			NoSuchPaddingException {
		cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(CHAVE.getBytes(),
				"AES"));
		byte[] decrypted = cipher.doFinal(critografa);
		return new String(decrypted);
	}
}
